package com.xiaomiMall.mange.service.Impl;

import com.xiaomiMall.mange.domain.XiaoMiProd;
import com.xiaomiMall.mange.domain.XiaoMiProdDetail;
import com.xiaomiMall.mange.domain.vo.Order;
import com.xiaomiMall.mange.domain.vo.XiaoMiProdOrderVo;
import com.xiaomiMall.mange.mapper.XiaoMiProdMapper;
import com.xiaomiMall.mange.service.XiaoMiProdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author codefish
 * @date 2021/11/2
 * @apinote
 */
//2021年11月14日09:31:40
@Service
public class XiaoMiProdServiceImpl implements XiaoMiProdService {

    @Autowired
    private XiaoMiProdMapper xiaoMiProdMapper;

    @Override
    public List<XiaoMiProd> getProdList(XiaoMiProd xiaoMiProd) {
        return xiaoMiProdMapper.getProdList(xiaoMiProd);
    }

    @Override
    public List<XiaoMiProdDetail> getProdDetailList(XiaoMiProd xiaoMiProd) {
        return xiaoMiProdMapper.getProdDetailList(xiaoMiProd);
    }

    @Override
    public boolean uploadProdPicture(String prodPicture, String prodId) {
        return xiaoMiProdMapper.uploadProdPicture(prodPicture, prodId);
    }

    @Override
    public List getProdColor(String uid, String prodId) {
        return xiaoMiProdMapper.getProdColor(uid, prodId);
    }

    @Override
    @Transactional
    public Integer addProduct(XiaoMiProd xiaoMiProd) {
        return xiaoMiProdMapper.addProduct(xiaoMiProd);
    }

    @Override
    @Transactional
    public boolean addProductDetail(List<XiaoMiProdDetail> xiaoMiProdDetailList) {
        boolean flag = true;
        int count = xiaoMiProdMapper.addProductDetail(xiaoMiProdDetailList);

        if (xiaoMiProdDetailList.size() != count){
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean updateProduct(XiaoMiProd xiaoMiProd, List<XiaoMiProdDetail> xiaoMiProdDetailList) {
        boolean flag = true;
        int attached_table_count = 0;
        //1.更新基本表数据
        int main_table_count = xiaoMiProdMapper.updateProduct(xiaoMiProd);

        //2.更新从表数据
        for (XiaoMiProdDetail xiaoMiProdDetail: xiaoMiProdDetailList) {
            int count = xiaoMiProdMapper.updateDetailProduct(xiaoMiProdDetail);
            attached_table_count += count;
        }

        if (main_table_count != 1){
            flag = false;
        }

        return flag;
    }

    @Override
    @Transactional
    public boolean changeProdStatus(String prodId, String status) {
        int count = xiaoMiProdMapper.changeProdStatus(prodId, status);
        if (count != 1){
            throw new RuntimeException("商品状态改变失败!");
        }
        return true;
    }

    @Override
    public List<XiaoMiProd> exportProduct(List<String> prodIds) {
        return xiaoMiProdMapper.exportProduct(prodIds);
    }

    @Override
    public List<XiaoMiProdOrderVo> getOrderData(Order prodOrder) {
        return xiaoMiProdMapper.getOrderData(prodOrder);
    }

    @Override
    public List<HashMap<String, String>>  getOrderSalesInfo() {
        return xiaoMiProdMapper.getOrderSalesInfo();
    }
}
